package com.barbeque.mall.util;

import com.barbeque.mall.bean.user.MarketUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;

/**
 * @author ycc
 * @date 2022/07/20 14:49:50
 */
public class UserInfoUtils {
    public static MarketUser user = null;

    public static final Integer ILLEGAL_USERID = -1;

    //获取用户信息
    public static int getUserId(){
        Subject subject = SecurityUtils.getSubject();
        try {
            user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        } catch (Exception e) {
            return ILLEGAL_USERID;
        }
        if (user!=null) {
            return user.getId();
        }
        //如果未登录或未获取登录时用户的信息，则不会显示相关数据
        return ILLEGAL_USERID;
    }
}
